<?php

	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'schedulePerSectionIrregular.html'));
	
	$CID 	= _GET('cid');
	$SID 	= _GET('sid');
	$HASH 	= _GET('hash');
	$LNK 	= _GET('lnk');
	$CHNG 	= _GET('chng');
	$SLID 	= _GET('slid');
	$CSID 	= _GET('csid');

	#SUBJ LIST
	$semSubjList = $STUD->getStudentSubjectTakenListSeperateBySem($logUserID, $CID, $SLID);
	
	if ( !empty($semSubjList) ) {
		$subjListHTML = '';
		$sectionHTML = '<div class="div-reg-schedule"><ul class="ul-reg-schedule">';
		
		foreach($semSubjList as $semSubjListField => $studentSubjectList) {
			$semDesc = $DSC->getSemesterDescViaLinker($semSubjListField);
			$subjListHTML .= '<table width="0" border="0" cellspacing="0" cellpadding="0" class="table-list-semester-subject">
								<tr>
									<td width="650" height="25" colspan="3" class="subject-taken-value-line">&nbsp;&nbsp;<span class="font-red"><strong>' . $semDesc['sem_desc'] . ' (' . $semDesc['sem_code'] . ')</span></strong></td>
									<td class="subject-taken-value-line">&nbsp;<strong>Total Hours</strong></td>
									<td class="subject-taken-value-line">&nbsp;<strong>Schedule</strong></td>
								</tr>
								<tr>
									<td height="5" colspan="5"></td>
								</tr>';
			if (!empty($studentSubjectList)) {
				$count = 1;
				$studentIrregularSubjectGrades = '';
				foreach($studentSubjectList as $studentSubjectListVal) {
					
					$studentIrregularSubjectGrades = $STUD->getIrregularSubjectSchedulesiFrame($logUserID, $CID, $SLID, $activeYear, $studentSubjectListVal['subj_idPK']);
					//r_print($studentIrregularSubjectGrades);
					$subjListHTML .= '<tr>
										  <td width="25" class="subject-taken-value">&nbsp;<strong>' . $count . '.</strong></td>
										  <td width="280" class="subject-taken-value">&nbsp;' . $studentSubjectListVal['subj_desc'] . '</td>
										  <td width="70" class="subject-taken-value">&nbsp;' . $studentSubjectListVal['subj_code'] . '</td>
										  <td width="110" class="subject-taken-value" align="left">&nbsp;&nbsp;&nbsp;<strong>' . abs($studentSubjectListVal['total_hrs']) . '</strong> hours</td>
										  <td width="110" class="subject-taken-value" align="left">&nbsp;&nbsp;&nbsp;' . $studentIrregularSubjectGrades . '</td>
									 </tr>';
					$count++;
				}
			}
			$subjListHTML .= '</table><div class="subj-list-spacer"></div>';
			
			
			$defaultSections = $STUD->getDefaultSections($CID, '', $semSubjListField);
			
			if (!empty($defaultSections)) {
				foreach($defaultSections as $defaultSectionsValue) {
					$sectionHTML .= '<li class="li-reg-schedule" onclick="showLoading(\'' . $defaultSectionsValue['section_idPK'] . '\'); linkNewPage(\'SchedulePerSectionIrregular&cid=' . $CID . '&csid=' . $CSID . '&hash=' . hashRandom($HASH . $defaultSectionsValue['section_desc']) . '&sid=' . $defaultSectionsValue['section_idPK'] . '&slid=' . $SLID . '&chng=1\')">' . $defaultSectionsValue['section_desc'] . ' <span id="loading_img' . $defaultSectionsValue['section_idPK'] . '" class="display-none"><img src="{dir_image}icon/loading_round.gif" border="0"/></span></li>';
				}
		
			}

		}
		$sectionHTML .= '</ul></div>' . $changeSection;
	}
	
	$getStudentSubjectListSemester = $STUD->getStudentSubjectListSemester($logUserID, $CID, $SLID);
	if ( !empty($getStudentSubjectListSemester) ) {
		foreach($getStudentSubjectListSemester as $getStudentSubjectListSemesterValue) {
			if ( empty($getStudentSubjectListSemesterValue['grade']) ) {
				$studSubjActiveList[] = $getStudentSubjectListSemesterValue['subj_idFK'];
			}
		}
	}
	
	$sectionsDescription = $STUD->getDefaultSections($CID, $SID, (empty($SID)) ? $SLID : '');
	$sectionDesc 		 =  $sectionsDescription[0]['section_desc'];
	
	if ( empty($SID) ) {
		$SID = $sectionsDescription[0]['section_idPK'];
	}
	
	
	/* - - - - - - - */
	
	$scheduleDefaultTime = $STUD->getScheduleDefaultTime(array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'), $activeYear);
	$timeCount  = 0;
	$bgcolor 	= LOOPING_F0F;
	if (!empty($scheduleDefaultTime)) {
		$scheduleHTML = $sectionHTML . '<table width="0" border="0" cellspacing="0" cellpadding="0" class="schedule-table">
						 <tr>
							<td width="120" height="25" class="schedule-label schedule-label-right" colspan="3">&nbsp;</td>
							<td width="105" class="schedule-label schedule-label-right">Monday</td>
							<td width="105" class="schedule-label schedule-label-right">Tuesday</td>
							<td width="105" class="schedule-label schedule-label-right">Wenesday</td>
							<td width="105" class="schedule-label schedule-label-right">Thursday</td>
							<td width="105" class="schedule-label schedule-label-right">Friday</td>
							<td width="105" class="schedule-label">Saturday</td>
						 </tr>';
		$timeScheduleFrom = $STUD->getTimeSchedule('sched_from', $activeYear, 1);
		$timeScheduleTo   = $STUD->getTimeSchedule('sched_to', $activeYear, 1);
		$classSubjActive = array();
		foreach($scheduleDefaultTime as $scheduleDayField => $scheduleDay) {
			$scheduleHTML .= '<tr align="center" ' . $bgcolor . '><td width="50" class="schedule-label-time"><div>' . date("h:i", strtotime($timeScheduleFrom[$timeCount])) . '</div><div style="font-weight:normal; font-style:italic; font-size:12px;">' . date("a", strtotime($timeScheduleFrom[$timeCount])) . '</div></td><td width="20" class="schedule-label-time"> - </td><td width="50" class="schedule-label-time schedule-label-right"><div>' . date("h:i", strtotime($timeScheduleTo[$timeCount])) . '</div><div style="font-weight:normal; font-style:italic; font-size:12px;">' . date("a", strtotime($timeScheduleTo[$timeCount])) . '</div></td>';
			$rightCount 		= 1;
			$countDay   		= count($scheduleDay);
			$classSubj 			= '';
			foreach($scheduleDay as $scheduleTimeField => $scheduleTime) {
				
				$clasRight = ($rightCount == $countDay) ? '' : 'schedule-time-right';
				if ($scheduleTime['sched_break'] == 'Y') {
					$sched = '<span class="font-red"><strong>LUNCH</strong></span>';
				}
				else {
					$studChangeSectionIrregSet 		   = $STUD->getStudChangeSectionIrregSet($logUserID, $scheduleTime['sched_time_idPK']);
					$sectionSubjectGenerateSchedules   = $STUD->getSectionSubjectGenerateSchedules($SID, $scheduleTime['sched_time_idPK'], $activeYear); # CHANGE
					$irregularSubjectGenerateSchedules = $STUD->getIrregularSubjectGenerateSchedules($logUserID, $CID, $SLID, $activeYear, $scheduleTime['sched_time_idPK'], false, $SID);
					$studentIrregularSubjectGrades 	   = $STUD->getIrregularSubjectSchedulesiFrame($logUserID, $CID, $SLID, $activeYear, $sectionSubjectGenerateSchedules['subj_idPK']);
					$classSubj = $SID . '-' . $sectionSubjectGenerateSchedules['subj_idPK'];
					$sched = '';
					//r_print($sectionSubjectGenerateSchedules);
					if ( !empty($sectionSubjectGenerateSchedules) ) {
						if ( in_array($sectionSubjectGenerateSchedules['subj_idPK'], $studSubjActiveList) ) {
							switch($sectionSubjectGenerateSchedules['lec_lab_both']) {
								case '0': $lecLabBoth = '(<em>lec</em>)'; break;
								case '1': $lecLabBoth = '(<em>lab</em>)'; break;
								case '2': $lecLabBoth = '(<em>lec/lab</em>)'; break;
							}
							if ( $sectionSubjectGenerateSchedules['is_saturday'] == 'N' ) {
								$sched .= '<div><strong>' . upperCase(substr($sectionSubjectGenerateSchedules['first_name'],0,1)) . '. ' . upperCase($sectionSubjectGenerateSchedules['last_name']) . '</strong></div>';
								$sched .= '<div>' . $sectionSubjectGenerateSchedules['room_desc'] . '</div>';
								$sched .= '<div>' . $lecLabBoth . '</div>';
							}
							$sched .= '<div><strong>' . $sectionSubjectGenerateSchedules['subj_code'] . '</strong></div>';
							//r_print($classSubjActive);
								if ( empty($irregularSubjectGenerateSchedules) ) {
									if ( $sectionSubjectGenerateSchedules['total_hrs'] > $studentIrregularSubjectGrades) {
										if ( $studChangeSectionIrregSet == 0 ) {
											$sched .= '<div class="schedule-active-value schedule-active-value-set display-none ' . $classSubj . '"
															id="time' . $scheduleTime['sched_time_idPK'] . '"
															onclick="clickSchedule(\'' . $scheduleTime['sched_time_idPK'] . '\',
																				   \'' . $logUserID . '\',
																				   \'' . $CID . '\',
																				   \'' . $SLID . '\',
																				   \'' . $activeYear . '\',
																				   \'' . $sectionSubjectGenerateSchedules['subj_idPK'] . '\',
																				   \'' . $sectionSubjectGenerateSchedules['prof_subj_idPK'] . '\',
																				   \'' . $SID . '\');">
																SET
														</div>';
										}
										else {
											$classSubjActive[] = $SID . '-' . $sectionSubjectGenerateSchedules['subj_idPK'];
										}
									}
									else {
										$sched .= '';
									}
								}
								else {
									if ( $irregularSubjectGenerateSchedules['is_accepted'] == 'P' ) {
										if ( $irregularSubjectGenerateSchedules['is_deleted'] == 'Y' ) {
											$sched .= '';
											$sched .= '<div class="schedule-active-value">[P] Added</div>';
										}
										else {
											$sched .= '<div class="schedule-active-value-irregular"></div>';
											$sched .= '<div class="schedule-active-value">[P] Deleted</div>';
										}
									}
									else {
										$sched .= '<div class="schedule-active-value-irregular"></div>';
										$sched .= '<div class="schedule-active-value display-none ' . $classSubj . '"
														id="time' . $scheduleTime['sched_time_idPK'] . '"
														onclick="clickDeleteSchedule(\'' . $logUserID . '\', \'' . $scheduleTime['sched_time_idPK'] . '\', \'' . $irregularSubjectGenerateSchedules['scsi_idPK'] . '\', \'' . $SLID . '\', \'' . $sectionSubjectGenerateSchedules['subj_idPK'] . '\');">
														DELETE
													</div>';
									}
								}
						}
						else {
							$sched = '&nbsp;';
						}
					}
					else {
						$sched = '&nbsp;';
					}
				}
				
				$scheduleHTML .= '<td class="schedule-time ' . $clasRight . '" height="75" align="center" onmouseover="setSchedule(\'' . $classSubj . '\', \'show\')" onmouseout="setSchedule(\'' . $classSubj . '\', \'hide\')"><div class="position-relative">' . $sched . '</div></td>';
				$rightCount++;
			}
			$scheduleHTML .= '</tr>';
			$bgcolor = ($bgcolor == LOOPING_F0F) ? LOOPING_FFF : LOOPING_F0F;
			$timeCount++;
		}
		$scheduleHTML .= '<tr><td height="5" class="schedule-label-right" colspan="3"></td><td colspan="6"></td></tr></table>';
	}
	else {
		$scheduleHTML = '<div class="change-section-button"><div class="set-time-schedule" onclick="setTimeSchedule(\'' . $activeYear . '\');">SET TIME FOR MONDAY - SATURDAY SCHEDULE</div></div><div id="set_time_schedule"></div>';
	}

	/* - - - - - - - */
	
	$getCourse 	  = $DSC->getCourse($CID);
	if(!empty($getCourse)) {
		$course_label	= $getCourse['course_desc'] . ' (' . $getCourse['course_code'] . ')';
	}
	
	$requestIrregularSchedule 			= $STUD->RequestIrregularSchedule($strAccNo, 'tbl_stud_change_section_irreg');
	$sendRequestIrregularScheduleCount  = $STUD->SendRequestIrregularScheduleCount($logUserID);
	
	if ( $requestIrregularSchedule > 0 ) {
		$requestButton = '<span class="font-grey"><em><strong>[PENDING REQUEST]</strong></em></span>';
	}
	else {
		if ( $sendRequestIrregularScheduleCount > 0 ) {
			$requestButton = '<input type="button" id="subAccept" class="submit-input-green" value="Send Request" onclick="submitRequestSectionForm()"/>
							  <input type="button" id="subCancel" onclick="cancelRequestSchedule();" class="submit-input-grey" value="Cancel"/>';
		}
	}
	
	
	$TEMP->set_var(array('prof_name' 		=> $profName,
						 'time_table'		=> $scheduleHTML,
						 'save_sched'		=> $saveSched,
						 'cid'				=> $CID,
						 'course_label'		=> $course_label,
						 'subjList_html'	=> $subjListHTML,
						 'section_desc'		=> $sectionDesc,
						 'sy_label'			=> $__SYR__,
						 'stud_id'			=> $logUserID,
						 'request_button'	=> $requestButton,
						 'subj_active'		=> rtrim(ltrim(str_replace('","', ',', (json_encode($classSubjActive))), '["'), '"]')
						));
?>